package com.gree.flowable.app.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gree.common.entity.ReturnVo;
import com.gree.flowable.app.mapper.FormInterfaceInfoMapper;
import com.gree.flowable.app.pojo.form.FormInterfaceInfo;
import com.gree.flowable.app.services.IFormInterfaceInfoService;
import com.gree.flowable.app.tools.constants.ReturnCode;
import com.gree.framework.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author ：180849
 * @date ：Created in 2021/5/24 11:09
 * @description：
 * @modified By：
 * @version: 1.0
 */
@Service
public class IFormInterFaceInfoServiceImpl implements IFormInterfaceInfoService {
    @Autowired
    private FormInterfaceInfoMapper formInterfaceInfoMapper;


    @Override
    @Transactional
    public ReturnVo addInfo(List<FormInterfaceInfo> formInterfaceInfoList) {
        ReturnVo result = null;
        Integer addTotal = 0;
        String systemName = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        if(systemName.equals("admin")){
            systemName = null;
        }
        Map<String, String> userDescMap = formInterfaceInfoMapper.getUserDesc(systemName);
        String userDesc = userDescMap.get("USERDESC");
        for (FormInterfaceInfo formInterfaceInfo : formInterfaceInfoList) {
            formInterfaceInfo.setUserName(systemName);
            formInterfaceInfo.setUserDesc(userDesc);
            addTotal = formInterfaceInfoMapper.insert(formInterfaceInfo);
        }
        if (addTotal > 0){
            result = new ReturnVo(ReturnCode.SUCCESS,"接口插入成功",addTotal);
        } else {
            result = new ReturnVo(ReturnCode.FAIL,"接口插入失败",addTotal);
        }
        return result;
    }

    @Override
    @Transactional
    public ReturnVo updateInterfaceInfo(List<FormInterfaceInfo> formInterfaceInfoList, String systemName) {
        Map<String, String> userDescMap = formInterfaceInfoMapper.getUserDesc(systemName);
        String userDesc = userDescMap.get("USERDESC");
        Integer updateTotal = 0;
        for (FormInterfaceInfo formInterfaceInfo : formInterfaceInfoList) {
            formInterfaceInfo.setUserDesc(userDesc);
            QueryWrapper<FormInterfaceInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(FormInterfaceInfo::getUserName,systemName).eq(FormInterfaceInfo::getInterfaceType,formInterfaceInfo.getInterfaceType());
            updateTotal = formInterfaceInfoMapper.update(formInterfaceInfo, queryWrapper);

        }

        ReturnVo result = null;
        if (updateTotal > 0){
            result = new ReturnVo(ReturnCode.SUCCESS,"数据更新成功",updateTotal);
        } else {
            result = new ReturnVo(ReturnCode.FAIL,"数据更新失败",updateTotal);
        }
        return result;
    }
}
